<?php
App::uses('AppController', 'Controller');

class UsersController extends AppController {

	var $helpers = array('Paginator','Html');
	var $components = array('Strings');
	public $user = "";
	public function beforeFilter() {
		parent::beforeFilter();
		
	}
	/**
	* admin index method
	*/
	public function admin_index() {
		$this->user = parent::checkAdminLogin();
		$conditions = array("User.deleted" => 0);
		$this->paginate = array(
                                'limit' => ITEM_PER_PAGE,
                                'order' => array('id' => 'asc'),
                             );
        $data = $this->paginate("User",$conditions);
        $this->set("data",$data);
		$this->set("title_for_layout","Danh sách người dùng");
	}
	/**
	* admin add method
	*/
	public function admin_add(){
		$this->user = parent::checkAdminLogin();
		if ($this -> request -> is('post')) {
			
			$new = $this -> request->data;
			// kiem tra trung user name , email
			if(!$this->User->isUniqueUsername($new) ||
					!$this->User->isUniqueEmail($data)){
				$this->Session->setFlash('Vui lòng nhập Tên, hoặc email khác');
				return;
			}
			if($new["password"] != $new["repassword"]){
				$this->Session->setFlash('Mật khẩu không chính xác');
				return;
			}
			$new['username'] = $this->Strings->replaceHTMLTag($new['username']);
			$new['group_id'] = GROUP_MANAGER;
			$new['deleted'] = 0;
			$new['created_by'] = $this->user['id'];
			$new['created_at'] = date("Y-m-d H:i:s");
			$new['updated_by'] = $this->user['id'];
			$new['updated_at'] = date("Y-m-d H:i:s");
			// lưu thành công
			if($this->User->save($new)){
				$this->Session->setFlash('Thêm user thành công');
				$this->redirect(array('action' => 'index'));
			}
		}		
	}

	/**
	* admin edit method
	*/
	public function admin_edit($id){
		$this->user = parent::checkAdminLogin();
		$this -> User -> id = $id;
		if (!$this -> User -> exists()) {
			$this -> Session -> setFlash('Tài khoản không hợp lệ');
			 $this -> redirect(array('action' => 'index'));
		}
		
		$conditions = array("User.id" => $id);
		$data = $this->User->find('first', array('conditions' => $conditions));
		$this -> set("data", $data);
		$this -> set("id",$id);
		
		//admin chỉ dc chinh sua GROUP_MANAGER
		if($data['User']['group_id'] != GROUP_MANAGER){
			$this->Session->setFlash('Bạn không có quyền chỉnh sửa thông tin nguoi dùng');
			$this->redirect(array('action' => 'index'));
		}
		
		if ($this -> request -> is('post')) {
			
			$new = $this -> request->data;
			$this->User->checkUserNameUpdate($new['username'],$id);
			
			// kiem tra trung user name , email
			if($this->User->checkUserNameUpdate($new['username'],$id) == 1 ||
					$this->User->checkUserEmailUpdate($new['email'],$id) == 1){
				$this->Session->setFlash('Vui lòng nhập Tên, hoặc email khác');
				return;
			}
			if($new["password"] != $new["repassword"]){
				$this->Session->setFlash('Mật khẩu không chính xác');
				return;
			}		
			
			if($new['password']==""){
				unset($new['password']);
			}
			$new['id'] = $id;
			$new['username'] = $this->Strings->replaceHTMLTag($new['username']);
			$new['group_id'] = GROUP_MANAGER;
			$new['updated_by'] = $this->user['id'];
			$new['updated_at'] = date("Y-m-d H:i:s");
			// lưu thành công
			if($this->User->save($new)){
				$this->Session->setFlash('Cập nhật user thành công');
				$this->redirect(array('action' => 'index'));
			}
		}	
	}
	/**
	* admin delete method
	*/
	
	public function admin_delete($id) {
		$this->user = parent::checkAdminLogin();
		$conditions = array("User.id" => $id);
		$data = $this->User->find('first', array('conditions' => $conditions));
		parent::createException($data['User']);
		$data['User']['deleted'] = 1;
		$data['User']['updated_at'] = date("Y-m-d H:i:s");
		
		if($this -> User -> save($data)){
		$this->Session->setFlash('Xóa thành công');
		}else{
			$this->Session->setFlash('Xóa không thành công');
		}
		return $this->redirect(array('controller' => 'Users', 'action' => 'index'));
	}
	public function admin_changepass(){
		$this->user = parent::checkAdminOrManagerLogin();
		$this->loadModel('User');
		$this->set("user",$this->user);
		parent::leftMember();
		$success = 0;
		$user = $this->user;
		if ($this->request->is('post')) {
			$data = $this->request->data;
			$data['User']['id'] = $user['id'];
			$this->User->set($data);
			$this->User->set($this->request->data);
			if ($this->User->validates()){
				if($this->update_password($user['id'])){
					$this->Session->setFlash('Đổi mật khẩu thành công', 'default', array('class'=>'alert alert-info'));
					$success = 1;
				}else{
					$this->Session->setFlash('Có lỗi xảy ra, vui lòng thử lại!', 'default', array('class'=>'alert alert-danger'));
				}
			}else{
				$this->set('errors',$this->User->validationErrors);
			}
		}
		$this->set('title_for_layout','Đổi mật khẩu');
		$this->set('Flat','changepass');
		$this->set("success",$success);
	}
	/**
	 * update_password - cập nhật mật khẩu
	 */
	private function update_password($id){
		$this->User->id = $id;
		//if($this->User->saveField('password',$this->Auth->password($this->request->data['User']['password']))){
		if($this->User->saveField('password',$this->request->data['User']['password'])){
			return true;
			
		}else{
			return false;
		}
	}
	/**
	* admin edit method
	*/
	public function admin_editadmin($id){
		$this->user = parent::checkAdminLogin();
		$this -> User -> id = $id;
		if (!$this -> User -> exists()) {
			$this -> Session -> setFlash('Tài khoản không hợp lệ');
			 $this -> redirect(array('action' => 'index'));
		}
		
		$conditions = array("User.id" => $id);
		$data = $this->User->find('first', array('conditions' => $conditions));
		$this -> set("data", $data);
		$this -> set("id",$id);
		
		//admin chỉ dc chinh sua GROUP_MANAGER
		if($data['User']['group_id'] != GROUP_ADMIN){
			$this->Session->setFlash('Bạn không có quyền chỉnh sửa thông tin nguoi dùng');
			$this->redirect(array('action' => 'index'));
		}
		
		if ($this -> request -> is('post')) {
			
			$new = $this -> request->data;
			$this->User->checkUserNameUpdate($new['username'],$id);
			
			// kiem tra trung user name , email
			if($this->User->checkUserNameUpdate($new['username'],$id) == 1 ||
					$this->User->checkUserEmailUpdate($new['email'],$id) == 1){
				$this->Session->setFlash('Vui lòng nhập Tên, hoặc email khác');
				return;
			}
			$new['id'] = $id;
			$new['username'] = $this->Strings->replaceHTMLTag($new['username']);
			$new['updated_by'] = $this->user['id'];
			$new['updated_at'] = date("Y-m-d H:i:s");
			// lưu thành công
			if($this->User->save($new)){
				$this->Session->setFlash('Cập nhật user thành công');
				$this->redirect(array('action' => 'index'));
			}
		}
	}
	
}
?>